package util

import (
	"encoding/json"
	"github.com/pkg/errors"
	"io"
)

func Decode(reader io.Reader, result interface{}) error {
	return json.NewDecoder(reader).Decode(result)
}

func ReadArgs(r io.Reader) (map[string]interface{}, error) {
	result := make(map[string]interface{})
	if err := json.NewDecoder(r).Decode(&result); err != nil {
		return nil, errors.WithMessage(err, "unmarshal args")
	}
	return result, nil
}

func ReadArgsTo(r io.Reader, result interface{}) error {
	if err := json.NewDecoder(r).Decode(result); err != nil {
		return errors.WithMessage(err, "unmarshal args")
	}
	return nil
}
